package com.zxsc.zxmusic.fragment;
import com.zxsc.zxmusic.R;
import com.zxsc.zxmusic.db.SongDb;
import com.zxsc.zxmusic.db.SongDb.OnRegisterLister;
import com.zxsc.zxmusic.manager.ThemeManager;
import com.zxsc.zxmusic.manager.ThemeManager.IThemeListener;
import com.zxsc.zxmusic.model.UserModel;
import com.zxsc.zxmusic.utils.ToastUtils;
import com.zxsc.zxmusic.utils.UserUtils;

import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class RegisterFragment extends BaseFragment implements OnClickListener,IThemeListener{
	
	private EditText etName,etPwd,etPwds,etSex;
	private Button btnReg;	
	
	@Override
	protected View inflateView(LayoutInflater inflater, ViewGroup container) {
		View view=LayoutInflater.from(getActivity()).inflate(R.layout.activity_register, container,false);
		etName=(EditText) view.findViewById(R.id.register_username);
		etPwd=(EditText) view.findViewById(R.id.register_userpwd);
		btnReg=(Button) view.findViewById(R.id.register_btnOk);
		etPwds=(EditText) view.findViewById(R.id.register_userpwds);
		etSex=(EditText) view.findViewById(R.id.register_sex);
		btnReg.setOnClickListener(this);
		 ThemeManager.with(getActivity()).registerListener(this);
		return view;
	}
	@Override
	public void onActivityCreated(@Nullable Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		initView();
	}
	private void initView() {
		// TODO Auto-generated method stub
		
	}
	@Override
	protected void onLoading() {
		// TODO Auto-generated method stub
		
	}
	@Override
	protected void onLoadFinish() {
		// TODO Auto-generated method stub
		setLoginThem(ThemeManager.with(getActivity()).getCurrentColor());
	}
	
	@Override
	public void onClick(View v) {
		final String name=etName.getText().toString()
				.trim();
		final String pwd=etPwd.getText()
				.toString().trim();
		
		String pwds=etPwds.getText().toString().trim();
		String sex=etSex.getText().toString().trim();
		
		if(name==null || name.length()<=0){
			ToastUtils.show(getActivity(), "用户名不能为空！");
			return;
		}
		if(pwd==null || pwd.length()<=0){
			ToastUtils.show(getActivity(), "密码不能为空！");
			return;
		}else{
			if(pwd.length()<6){
				ToastUtils.show(getActivity(), "密码位数不能低于6位！");
				return;
			}
		}
		if(pwds==null || pwds.length()<=0){
			ToastUtils.show(getActivity(), "请再次输入密码！");
			return;
		}else{
			if(!pwds.equals(pwd)){
				ToastUtils.show(getActivity(), "两次输入密码不相同！");
				return;
			}
		}

			
			final UserModel userModel=new UserModel(Base64.encodeToString((name+pwd).getBytes(), Base64.DEFAULT), name, pwd);
			SongDb.register(getActivity(), userModel, new OnRegisterLister() {
				
				@Override
				public void onSuccess() {
					ToastUtils.show(getActivity(), "注册成功",true);
					etName.setText("");
					etPwd.setText("");
					etPwds.setText("");
					etSex.setText("");
				}
				
				@Override
				public void onFail(String msg) {
					// TODO Auto-generated method stub
					ToastUtils.show(getActivity(), msg);
				}
			});
	}
	
	private void setLoginThem(int color){
		etName.setHintTextColor(color);
		etPwd.setHintTextColor(color);
		GradientDrawable shapeDrawable=(GradientDrawable) btnReg.getBackground();
		shapeDrawable.setColor(color);
	}
	@Override
	public void onThemeChange(int color) {
		setLoginThem(color);
	}

}
